<template>
  <splitpanes class="default-theme">
    <pane size="10" :min-size="0" max-size="50">
      <el-table ref="tableRef" v-loading="loading" :data="dataSource" highlight-current-row border :height="autoHeight" @row-click="handleRowClick" @select="selectSingle" @select-all="selectAll" @selection-change="selectAll" >
        <el-table-column label="名称" prop="outName" align="center"></el-table-column>
      </el-table>

      <div class="lab-table-page">
        <div style="display: inline-flex">
          <el-pagination
              :disabled="loading"
              :current-page="pageInfo.page"
              :page-sizes="[5, 10, 15, 20, 50, 100]"
              :page-size="pageInfo.pageSize"
              layout="total,prev, next"
              :total="pageInfo.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
          />
          <el-button-group style="padding: 2px 5px">
            <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
          </el-button-group>
        </div>
      </div>
    </pane>
    <pane size="90" :min-size="0" max-size="100">
      <el-tabs v-if="excelOuts.length > 0" v-model="activeOut" closable @tab-remove="tabRemove">
        <el-tab-pane v-for="excelOut in excelOuts" :key="excelOut.outId" :name="excelOut.outId" :label="excelOut.outName">
          <result-table :ref="excelOut.outId" :excel-out="excelOut"></result-table>
        </el-tab-pane>
      </el-tabs>
      <div v-else>未选择查询任务</div>
    </pane>
  </splitpanes>
</template>

<script>
import {Pane, Splitpanes} from "splitpanes";
import IdeaTable from "@/mixins/IdeaTable";
import ResultTable from "@/views/bl/ResultTable";

export default {
  name: "SearchTable",
  components: {ResultTable,Splitpanes, Pane},
  mixins: [IdeaTable],
  props: {
    excelTemplate: {
      type: Object,
      default: function () {
        return { excelId: 0 }
      }
    }
  },
  computed: {
    autoHeight(){
      return `calc(100vh - 160px)`
    }
  },
  data(){
    return {
      excelOuts: [],
      activeOut: ''
    }
  },
  created() {
    this.queryUrl = 'excelOut/queryPage'
    this.rowKey = 'outId'
    this.tableRef = 'tableRef'
    this.selectCount = 1
    this.addFilter({ field: 'excelId', op: '=', value: this.excelTemplate.excelId })
    this.pageInfo.sorts = [ { field: 'outId', asc: false } ]
    this.loadSource()
  },
  methods: {
    tabRemove(name) {
      const index = this.excelOuts.findIndex(item => item.outId === name)
      if (index >= 0) {
        this.excelOuts.splice(index, 1)
      }
      if (this.excelOuts.length > 0) {
        this.activeOut = this.excelOuts[0].outId
      } else {
        this.activeOut = ''
      }
    },
    rowClick(row){
      const index = this.excelOuts.findIndex(item => item.outId === row.outId)
      if (index < 0) {
        this.excelOuts.push(row)
      }
      this.activeOut = row.outId
    },
    loadComplate() {
      const { roles, groupAdmin , systemAdmin } = this.loginUser
      if (groupAdmin || systemAdmin) {
        return
      }
      if (!roles) {
        this.dataSource.splice(0, this.dataSource.length)
      } else {
        // 化学指标
        let index = roles.findIndex(item => item.roleCode === 'QUERY_1')
        if (index === -1) {
          index = this.dataSource.findIndex(item => item.outName === '化学指标')
          if (index > -1) {
            this.dataSource.splice(index, 1)
            this.pageInfo.total--
          }
        }
        // 异常数据库
        index = roles.findIndex(item => item.roleCode === 'QUERY_CATCH')
        if (index === -1) {
          index = this.dataSource.findIndex(item => item.outName === '异常数据库')
          if (index > -1) {
            this.dataSource.splice(index, 1)
            this.pageInfo.total--
          }
        }
        // 三普标准样品评价表
        index = roles.findIndex(item => item.roleCode === 'QUERY_STANDARD')
        if (index === -1) {
          index = this.dataSource.findIndex(item => item.outName === '三普标准样品评价表')
          if (index > -1) {
            this.dataSource.splice(index, 1)
            this.pageInfo.total--
          }
        }
        // 三普平行样品评价表
        index = roles.findIndex(item => item.roleCode === 'QUERY_PARALLEL')
        if (index === -1) {
          index = this.dataSource.findIndex(item => item.outName === '三普平行样品评价表')
          if (index > -1) {
            this.dataSource.splice(index, 1)
            this.pageInfo.total--
          }
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
